package com.example.mp3player01;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
	
	private static final int ABOUT = 2;
	private static final int EXIT = 1;;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		TabHost tabHost = getTabHost();
		Intent remoteIntent = new Intent();
		remoteIntent.setClass(this, Mp3ListActivity.class);
		TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");
		
		Resources res = getResources();
		remoteSpec.setIndicator("Remote", res.getDrawable(android.R.drawable.stat_sys_download)); 
		remoteSpec.setContent(remoteIntent);
		tabHost.addTab(remoteSpec);
		
		
		//second tab
		Intent localIntent = new Intent();
		localIntent.setClass(this, LocalMp3ListActivity.class);
		TabHost.TabSpec localSpec = tabHost.newTabSpec("Local"); 
		localSpec.setIndicator("Local", res.getDrawable(android.R.drawable.stat_sys_upload));
		localSpec.setContent(localIntent);
		tabHost.addTab(localSpec);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, EXIT, 1, R.string.mp3list_exit);
		menu.add(0, ABOUT, 2, R.string.mp3list_about);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == ABOUT) {
			displayAboutAlert();
		} else if (item.getItemId() == EXIT) {
			displayExitAlert();
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	private void displayAboutAlert() {
		 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(MainActivity.this);
		 myAlertDialog.setTitle("--- About Page ---");
		 myAlertDialog.setMessage("This is a mp3 player android app version 1.");
		 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			  	public void onClick(DialogInterface arg0, int arg1) {
			  		//dismiss alert, just put nothing here.
			  	}
		  	}
		 );
		 myAlertDialog.show();
	}
	
	private void displayExitAlert() {
		 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(MainActivity.this);
		 myAlertDialog.setMessage("Do you want to exit ?");
		 myAlertDialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
			  public void onClick(DialogInterface arg0, int arg1) {
			  // do something when the Cancel button is clicked
			  }
		  }
		);
		 
		 myAlertDialog.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
			  	public void onClick(DialogInterface arg0, int arg1) {
			  		finish();
			  	}
		  	}
		 );
		 
		 myAlertDialog.show();
	}
	
}



